//
//  UserButton.m
//  Home
//
//  Created by 吴军虎吴军虎 on 16/4/26.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "UserButton.h"

#define AUTO_WIDTH  SCREEN_WIDTH / 375.f
#define AUTO_HEIGHT SCREEN_WIDTH / 375.f
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define AUTO_HEIGHTS SCREEN_HEIGHT / 667.f


@interface UserButton()

@property (nonatomic,strong) UIImageView *icoImage;
@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong)UIView *testView;
@property(nonatomic,strong)NSTimer *timer ;


@end

@implementation UserButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = GH;
        [self createSubView];
        [self setup1];
    }
    return self;
}

- (void)createSubView {
    
    self.icoImage = [[UIImageView alloc] init];
    [self addSubview:_icoImage];
    [self.icoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(@0);
        make.top.mas_equalTo(@0);
        make.size.mas_equalTo(CGSizeMake(60 * AUTO_WIDTH, 60 * AUTO_WIDTH));
    }];
    
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.font = [UIFont systemFontOfSize:16];
    self.nameLabel.textColor=[UIColor whiteColor];
    [self addSubview:_nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(@0);
        make.top.equalTo(self.icoImage.mas_bottom).offset(13 * AUTO_HEIGHTS);
        make.right.left.bottom.mas_equalTo(@0);
    }];
    
    [self addTarget:self action:@selector(selfTouchDown) forControlEvents:UIControlEventTouchDown];
    [self addTarget:self action:@selector(selfTouchUpInside) forControlEvents:UIControlEventTouchUpOutside];
}

- (void)setTitleStr:(NSString *)titleStr {
    
    _titleStr = titleStr;
    [self selfTouchUpInside];
    self.nameLabel.text = _titleStr;
}

- (void)selfTouchDown {
    
    [self setup];
    if ([_titleStr isEqualToString:@"我是设计师"]) {
        
        self.icoImage.image = [UIImage imageNamed:@"gong"];
       
    } else {
     
        self.icoImage.image = [UIImage imageNamed:@"zuo"];
    
    }
    
}

- (void)selfTouchUpInside {
    
    [self setup1];
    if ([_titleStr isEqualToString:@"我是设计师"]) {
       
        self.icoImage.image = [UIImage imageNamed:@"gongo"];
        
    } else {
    
        self.icoImage.image = [UIImage imageNamed:@"zuoo"];
    }
    
}


- (void)setup
{
   
    _testView=[[UIView alloc] initWithFrame:CGRectMake(-49, -58, 180, 180)];
    [self addSubview:_testView];
    
    _testView.layer.backgroundColor = [UIColor clearColor].CGColor;
    _testView.userInteractionEnabled=NO;
    //CAShapeLayer和CAReplicatorLayer都是CALayer的子类
    //CAShapeLayer的实现必须有一个path，可以配合贝塞尔曲线
    
    CAShapeLayer *pulseLayer = [CAShapeLayer layer];
    pulseLayer.frame = _testView.layer.bounds;
    pulseLayer.path = [UIBezierPath bezierPathWithOvalInRect:pulseLayer.bounds].CGPath;
    pulseLayer.fillColor = [UIColor whiteColor].CGColor;//填充色
    pulseLayer.opacity = 0.0;
    
    //可以复制layer
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    replicatorLayer.frame = _testView.bounds;
    replicatorLayer.instanceCount = 0;//创建副本的数量,包括源对象。
    replicatorLayer.instanceDelay = 1;//复制副本之间的延迟
    [replicatorLayer addSublayer:pulseLayer];
    [_testView.layer addSublayer:replicatorLayer];
    
    CABasicAnimation *opacityAnima = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnima.fromValue = @(0.3);
    opacityAnima.toValue = @(0.0);
    
    CABasicAnimation *scaleAnima = [CABasicAnimation animationWithKeyPath:@"transform"];
    scaleAnima.fromValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 0.0, 0.0, 0.0)];
    scaleAnima.toValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 1.0, 1.0, 0.0)];
    
    CAAnimationGroup *groupAnima = [CAAnimationGroup animation];
    groupAnima.animations = @[opacityAnima, scaleAnima];
    groupAnima.duration = 0.5;
    groupAnima.autoreverses = NO;
    groupAnima.repeatCount = HUGE;
    [pulseLayer addAnimation:groupAnima forKey:@"groupAnimation"];
   
    
     [self bringSubviewToFront:self.icoImage];
}

- (void)setup1
{
    
    _testView=[[UIView alloc] initWithFrame:CGRectMake(-49, -58, 180, 180)];
    [self addSubview:_testView];
    
    _testView.layer.backgroundColor = [UIColor clearColor].CGColor;
    _testView.userInteractionEnabled=NO;
    //CAShapeLayer和CAReplicatorLayer都是CALayer的子类
    //CAShapeLayer的实现必须有一个path，可以配合贝塞尔曲线
    
    CAShapeLayer *pulseLayer = [CAShapeLayer layer];
    pulseLayer.frame = _testView.layer.bounds;
    pulseLayer.path = [UIBezierPath bezierPathWithOvalInRect:pulseLayer.bounds].CGPath;
    pulseLayer.fillColor = [UIColor whiteColor].CGColor;//填充色
    pulseLayer.opacity = 0.0;
    
    //可以复制layer
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    replicatorLayer.frame = _testView.bounds;
    replicatorLayer.instanceCount = 0;//创建副本的数量,包括源对象。
    replicatorLayer.instanceDelay = 1;//复制副本之间的延迟
    [replicatorLayer addSublayer:pulseLayer];
    [_testView.layer addSublayer:replicatorLayer];
    
    CABasicAnimation *opacityAnima = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnima.fromValue = @(0.3);
    opacityAnima.toValue = @(0.0);
    
    CABasicAnimation *scaleAnima = [CABasicAnimation animationWithKeyPath:@"transform"];
    scaleAnima.fromValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 0.0, 0.0, 0.0)];
    scaleAnima.toValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 1.0, 1.0, 0.0)];
    
    CAAnimationGroup *groupAnima = [CAAnimationGroup animation];
    groupAnima.animations = @[opacityAnima, scaleAnima];
    groupAnima.duration = 1.0;
    groupAnima.autoreverses = NO;
    groupAnima.repeatCount = HUGE;
    [pulseLayer addAnimation:groupAnima forKey:@"groupAnimation"];
   
    //[pulseLayer removeFromSuperlayer];
    //[replicatorLayer removeFromSuperlayer];
    
    [self bringSubviewToFront:self.icoImage];
}


- (void)removeAnima:(CALayer *)layer
{
    [layer removeFromSuperlayer];
    [layer removeAnimationForKey:@"groupAnnimation"];
    
    //self.timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(action) userInfo:nil repeats:NO];
    
}

@end
